package org.linlinjava.litemall.admin.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.task.CronTaskRegistrar;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallShopMerchandiseJob;
import org.linlinjava.litemall.db.service.LitemallShopMerchandiseJobService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
/**
 * 管理店铺商品工作控制器
 *
 * @author yliyun
 * @date 2022/11/25
 */
@Api(tags = "门店货品预警API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/shopMerchandiseJob")
public class AdminShopMerchandiseJobController {

    /**
     * 商店商品工作服务
     */
    private final LitemallShopMerchandiseJobService shopMerchandiseJobService;
    private final CronTaskRegistrar cronTaskRegistrar;

    /**
     * 创建mer工作
     *
     * @param shopMerchandiseJob 商店商品工作
     * @return {@link ResponseUtil.Response}
     */
    @ApiOperation("创建定时预警")
    @PostMapping("createMerJob")
    @LogAnno
    public ResponseUtil.Response createMerJob(@Validated @RequestBody LitemallShopMerchandiseJob shopMerchandiseJob){
        long count = shopMerchandiseJobService.count();
        if(count>7){
            return ResponseUtil.fail("任务数量不能超过7个!");
        }
        long id = IdUtil.createSnowflake(1, 1).nextId();
        shopMerchandiseJob.setId(id);
        shopMerchandiseJob.setStatus(0);
        shopMerchandiseJob.setCreateTime(LocalDateTime.now());
        shopMerchandiseJob.setUpdateTime(LocalDateTime.now());
        return shopMerchandiseJobService.save(shopMerchandiseJob)?
                ResponseUtil.ok(shopMerchandiseJob):ResponseUtil.fail("创建失败");
    }

    /**
     * 删除mer工作
     *
     * @param id id
     * @return {@link ResponseUtil.Response}
     */
    @ApiOperation("删除定时任务")
    @GetMapping("deleteMerJob")
    @ApiImplicitParam(name = "id",value = "任务id")
    @LogAnno
    public ResponseUtil.Response deleteMerJob(@RequestParam("id") Long id){
        LitemallShopMerchandiseJob shopMerchandiseJob = shopMerchandiseJobService.getById(id);
        if(shopMerchandiseJob.getStatus()==2){
            return ResponseUtil.fail("当前任务正在执行，请先关闭！");
        }
        return shopMerchandiseJobService.removeById(id)?
                ResponseUtil.ok("删除成功"):ResponseUtil.fail("删除失败");
    }

    /**
     * 所有mer工作
     *
     * @return {@link ResponseUtil.Response}
     */
    @ApiOperation("任务集合")
    @GetMapping("allMerJob")
    @LogAnno
    public ResponseUtil.Response allMerJob(){
        return ResponseUtil.ok(shopMerchandiseJobService.list(Wrappers.lambdaQuery(LitemallShopMerchandiseJob.class)
                .orderByDesc(LitemallShopMerchandiseJob::getUpdateTime)));
    }

    /**
     * 查看单个任务
     *
     * @return {@link ResponseUtil.Response}
     */
    @ApiOperation("查看单个任务")
    @GetMapping("findByIdMerJob")
    @ApiImplicitParam(name = "id",value = "任务id")
    @LogAnno
    public ResponseUtil.Response findByIdMerJob(@RequestParam("id") Long id){
        return ResponseUtil.ok(shopMerchandiseJobService.getById(id));
    }

    /**
     * 开启关闭 任务
     *
     * @param shopMerchandiseJob 商店商品工作
     * @return {@link ResponseUtil.Response}
     */
    @ApiOperation("更新定时预警")
    @PostMapping("updateMerJob")
    @LogAnno
    public ResponseUtil.Response updateMerJob(@RequestBody LitemallShopMerchandiseJob shopMerchandiseJob){
        shopMerchandiseJob.setUpdateTime(LocalDateTime.now());
        if(shopMerchandiseJob.getStatus()==2){
            cronTaskRegistrar.schedulerAdd(shopMerchandiseJob);
        }else {
            cronTaskRegistrar.schedulerDelete(shopMerchandiseJob.getId().toString());
        }
        return shopMerchandiseJobService.updateById(shopMerchandiseJob)?
                ResponseUtil.ok("执行成功"):ResponseUtil.fail("执行失败");
    }

}
